<?php
namespace app\api\controller\v1\model\snapshot;

use think\Model;
use think\Db;


class Snapshot extends model
{
    //返回随手拍主页面
    public function getSnapShotImg(){

        $id = Db::query("select distinct circle_id from ym_snapshot_posts ORDER BY circle_id ASC");
        $length = count($id);
        $arr = array();
        for ($i=0; $i < $length; $i++) {
            $circle_id = $id[$i]['circle_id'];
            $result = Db::query("select a.circle_id,a.circle_title,a.circle_subtitle,a.circle_img,count(distinct b.user_id) as count 
                                    from ym_snapshot_circle as a 
                                    join ym_snapshot_posts as b on a.circle_id = b.circle_id 
                                    where b.circle_id = '$circle_id'");
            array_push($arr,$result[0]);
        }
        return $arr;

    }

//转发随手拍
    public function postsForward($post_id,$user_id,$super_user_id){
        $res = Db::query("select user_name,user_icon from user_info where user_id = '$user_id'");
        $super_res = Db::query("select user_name,user_icon from user_info where user_id = '$super_user_id'");

        $posts = Db::query("select *from ym_snapshot_posts where post_id = '$post_id' and user_id = '$super_user_id'");
        foreach ($res as $rows) {}
        $user_name = $rows['user_name'];
        $user_icon = $rows['user_icon'];

        foreach ($super_res as $super_rows) {}
        $super_user_name = $super_rows['user_name'];
        $super_user_icon = $super_rows['user_icon'];

        foreach ($posts as $post) {}
        $content = $post['content'];
        $circle_id = $post['circle_id'];

        $circles = Db::query("select *from ym_snapshot_circle where circle_id = '$circle_id'");

        foreach ($circles as $new) {}
        $title = $new['circle_title'];
        $sub_title = $new['circle_subtitle'];
        $img = $new['circle_img'];

        $time = date("Y-m-d h:i:s");
        $forward_user = array('名称' =>$user_name,'头像' =>$user_icon);
        $user = array('名称' => $super_user_name,'头像' => $super_user_icon,'内容' => $content,'时间'=> $time);
        $arr = array('转发者' =>  $forward_user,  '被转发者' => $user);

        Db::query("insert into ym_snapshot_forward values(null,'$post_id','$user_id','$super_user_id',now())");

        Db::query("insert into ym_snapshot_posts values(null,'$circle_id','$user_id','$content',now())");

        Db::query("insert into ym_snapshot_circle values(null,'$title','$sub_title','$img')");

        return $arr;
    }

//显示随手拍评论
    public function getShowPostCmmt($user_id,$post_id,$pageno){
        $pagesize = 10;
        $startno = $pageno *$pagesize;
        $result = Db::query("select a.cmmt_id,a.cmmt_content,a.create_time,b.user_id,b.user_name,b.small_user_icon as user_icon,c.cmmt_upvoteCount from ym_snapshot_cmmt as a join user_info as b on a.user_id = b.user_id join ym_cmmt_upvoteCount as c on a.cmmt_id = c.cmmt_id where a.post_id = '$post_id' ORDER BY c.cmmt_upvoteCount desc,a.create_time desc limit {$startno} , {$pagesize}");
        $length = count($result);
        for ($i=0; $i <$length ; $i++) {
            $cmmt_id = $result[$i]['cmmt_id'];
            $count = Db::query("select count(*) as count from ym_snapshot_cmmt where post_id = '$post_id'");
            $result[$i]['cmmt_count'] = $count[0]['count'];
            $super = Db::query("select super_user_id from ym_snapshot_cmmt where cmmt_id = '$cmmt_id'");
            $super_user_id = $super[0]['super_user_id'];
            if (!is_null($super_user_id)) {
                $user = Db::query("select user_id as super_user_id,user_name from user_info where user_id = '$super_user_id'");
                $result[$i]['super_user_id'] = $user[0]['super_user_id'];
                $result[$i]['super_user_name'] = $user[0]['user_name'];
            }else{
                $result[$i]['super_user_id'] = null;
                $result[$i]['super_user_name'] = null;
            }
            $upvote = Db::query("select cmmt_upvote from ym_snapshot_cmmt_upvote where cmmt_id = '$cmmt_id' and user_id = '$user_id'");
            // echo $upvote[0]['cmm_upvote'];
            if (is_null($upvote[0]['cmmt_upvote'])) {
                $result[$i]['cmmt_upvote'] = 0;
            }else{
                $result[$i]['cmmt_upvote'] = $upvote[0]['cmmt_upvote'];
            }
        }
        return $result;
    }


//随手拍热点话题
    public function getHotPost($user_id,$circle_id,$pageno,$type){

        $startno = $pageno * 3;
        if ($type == 'hot') {
            $posts = Db::query("select distinct b.user_id,b.user_name,b.user_icon,c.circle_title,c.circle_subtitle,c.circle_desc,c.circle_big_img,a.post_id,a.content,a.post_latitude,a.post_longitude,a.user_address,a.send_time,e.upvoteCount 
                                from ym_snapshot_posts as a 
                                left join user_info as b on a.user_id=b.user_id 
                                left join ym_snapshot_circle as c on a.circle_id=c.circle_id 
                                join ym_posts_upvoteCount as e on a.post_id = e.post_id  
                                where  c.circle_id = '$circle_id' 
                                ORDER BY e.upvoteCount 
                                desc limit {$startno},3");
            $length = count($posts);
            for ($i=0; $i < $length; $i++) {
                $post_id = $posts[$i]['post_id'];
                $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id'");
                $posts[$i]['img_urls'] = $img;
                $count = Db::query("select count(*) as count from ym_snapshot_cmmt where post_id = '$post_id'");
                $posts[$i]['cmmt_count'] = $count[0]['count'];
                $upvote = Db::query("select upvote from ym_snapshot_posts_upvote where post_id = '$post_id' and user_id = '$user_id'");
                if (is_null($upvote[0]['upvote'])) {
                    $posts[$i]['upvote'] = 0;
                }else{

                    $posts[$i]['upvote'] = $upvote[0]['upvote'];
                }
            }
            return $posts;
        }elseif ($type == 'new') {
            $posts = Db::query("select distinct b.user_id,b.user_name,b.user_icon,c.circle_title,c.circle_subtitle,c.circle_desc,c.circle_big_img,a.post_id,a.content,a.post_latitude,a.post_longitude,a.user_address,a.send_time,e.upvoteCount 
                                from ym_snapshot_posts as a 
                                left join user_info as b on a.user_id=b.user_id 
                                left join ym_snapshot_circle as c on a.circle_id=c.circle_id 
                                join ym_posts_upvoteCount as e on a.post_id = e.post_id  
                                where c.circle_id = '$circle_id' 
                                ORDER BY a.send_time 
                                desc limit {$startno},3");
            $length = count($posts);
            for ($i=0; $i < $length; $i++) {
                $post_id = $posts[$i]['post_id'];
                $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id'");
                $posts[$i]['img_urls'] = $img;
                $count = Db::query("select count(*) as count from ym_snapshot_cmmt where post_id = '$post_id'");
                $posts[$i]['cmmt_count'] = $count[0]['count'];

                $upvote = Db::query("select upvote from ym_snapshot_posts_upvote where post_id = '$post_id' and user_id = '$user_id'");
                if (is_null($upvote[0]['upvote'])) {
                    $posts[$i]['upvote'] = 0;
                }else{

                    $posts[$i]['upvote'] = $upvote[0]['upvote'];
                }
            }
            return $posts;
        }
    }

    //显示随手拍单个话题帖子
    public function getShowSnapshotPost($post_id,$user_id){
        $post = Db::query("select b.user_id,b.user_name,b.user_icon,a.post_id,a.content,a.post_latitude,a.post_longitude,a.user_address,a.send_time,c.upvoteCount from ym_snapshot_posts as a left join user_info as b on a.user_id=b.user_id join ym_posts_upvoteCount as c on a.post_id = c.post_id where a.post_id = '$post_id' ");

        $length = count($post);
        for ($i=0; $i < $length; $i++) {
            $post_id = $post[$i]['post_id'];
            $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id'");
            $post[$i]['img_urls'] = $img;
            $count = Db::query("select count(*) as count from ym_snapshot_cmmt where post_id = '$post_id'");
            $post[$i]['cmmt_count'] = $count[0]['count'];
            $upvote = Db::query("select upvote from ym_snapshot_posts_upvote where post_id = '$post_id' and user_id = '$user_id'");
            if (is_null($upvote[0]['upvote'])) {
                $post[$i]['upvote'] = 0;
            }else{
                $post[$i]['upvote'] = $upvote[0]['upvote'];
            }
        }
        return $post;
    }






















}